package TeacherServlet;

import Navigator.student.StudentDao;
import Navigator.student.StudentEnity;
import TeachLesson.TeachLessonsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

//学生选课
@WebServlet(name = "AddStudentLessonServlet")
public class AddStudentLessonServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String nums=request.getParameter("nums");
        String courseNo=request.getParameter("courseNo");
        TeachLessonsDao teachLessonsDao=new TeachLessonsDao();
        int row;
        //判断添加学生方式
        switch (nums){
            case "1":    //添加单个学生
                String studentNo=request.getParameter("studentNo"); //获取学号
                String workerNo=request.getParameter("workerNo"); //获取工号
                row=teachLessonsDao.addStudentLesson(studentNo,courseNo,workerNo);//执行添加
                if(row>0){
                    response.sendRedirect("/Teachers/AddStudentLesson.jsp");
                }else {
                    response.getWriter().println("添加失败,请后退重试1");
                }
                break;
            case "2":   //按班级添加
                String className=request.getParameter("className");
                String workerNo2=request.getParameter("workerNo");
                ArrayList<StudentEnity> a=new StudentDao().getStudentNoByClass(className);
                for (StudentEnity temp:a){
                    response.getWriter().println(temp.getStudentNo());
                }
                 row=teachLessonsDao.addStudentByClass(className,courseNo,workerNo2);
              if(row>0){
                     response.sendRedirect("/Teachers/AddStudentLesson.jsp");
                 }else {
                     response.getWriter().println("添加失败请重试");
                 }
                break;
        }
    }
}
